import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class SimScheduler {

	private static ArrayList<String> scheduleDetails;
	private static ArrayList<String> detailedScheduling;
	private static ArrayList<String> processStatistics;
	private static boolean statsOnly;
	
	public static void main(String[] args) {
		scheduleDetails = new ArrayList<String>();
		detailedScheduling = new ArrayList<String>();
		processStatistics = new ArrayList<String>();
		// java SimScheduler <input_file> <output_file> [flags] NOTE: you can have multiple flags OR you may HAVE NONE
		
		File input = new File(args[0]);
		try {
			Scanner in = new Scanner(input);
			while(in.hasNext()){
				scheduleDetails.add(in.nextLine());
			}
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// this is where the flags will take place and there may be ZERO or MORE
		// first we check if any of the flags are the -statsonly because if there is one then it has to TURN OFF statistics
		for(int i=2; i < args.length; i++) {
			String flag = args[i];
			if(flag.equals("-statsonly")){
				//the output file will NOT contain scheduling decisions, only statistics.
				statsOnly = true;
			}
		}
		
		FirstComeFirstServe tuple;
		RoundRobin tuple2;
		File output = new File(args[1]);
		BufferedWriter out;
		try {
			out = new BufferedWriter(new FileWriter(output));
			for(int i=2; i < args.length; i++) {
				String flag = args[i];
				if(flag.equals("-FCFS")){
					//the program will run the FCFS algorithm
					out.write("FCFS");
					out.newLine();
					tuple = new FirstComeFirstServe(scheduleDetails);
					detailedScheduling = tuple.getDetailedScheduling();
					processStatistics = tuple.getProcessStatistics();
					for(int j=0; j < detailedScheduling.size(); j++) { //Make sure to turn this off with -statsOnly
						out.write(detailedScheduling.get(j));
						
					}
					out.newLine();
					for(int j=0; j < processStatistics.size(); j++) { 
						out.write(processStatistics.get(j));
						out.newLine();
					}
					out.write(tuple.getAlgorithmStatistics());
					out.newLine();
					out.newLine();
					tuple = null;
				}
				if(flag.equals("-RR1")){
					//the program will run the RR algorithm with the first specified quanta
					out.write("RR1");
					out.newLine();
					tuple2 = new RoundRobin(scheduleDetails, 10);
					detailedScheduling = tuple2.getDetailedScheduling();
					processStatistics = tuple2.getProcessStatistics();
					for(int j=0; j < detailedScheduling.size(); j++) { //Make sure to turn this off with -statsOnly
						out.write(detailedScheduling.get(j));
						
					}
					out.newLine();
					for(int j=0; j < processStatistics.size(); j++) { 
						out.write(processStatistics.get(j));
						out.newLine();
					}
					out.write(tuple2.getAlgorithmStatistics());
					out.newLine();
					out.newLine();
					tuple2 = null;
				}
				if(flag.equals("-RR2")){
					// The program will run the RR algorithm with the second specified quanta
					out.write("RR2");
					out.newLine();
					tuple2 = new RoundRobin(scheduleDetails, 50);
					detailedScheduling = tuple2.getDetailedScheduling();
					processStatistics = tuple2.getProcessStatistics();
					for(int j=0; j < detailedScheduling.size(); j++) { //Make sure to turn this off with -statsOnly
						out.write(detailedScheduling.get(j));
						
					}
					out.newLine();
					for(int j=0; j < processStatistics.size(); j++) { 
						out.write(processStatistics.get(j));
						out.newLine();
					}
					out.write(tuple2.getAlgorithmStatistics());
					out.newLine();
					out.newLine();
					tuple2 = null;
				}
				if(flag.equals("-MLFQ")){
					// The program will run the MLFQ algorithm
					out.write("MLFQ");
					out.newLine();
				}
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
